<?php
/**
 * User: 魔十七
 * Date: 2020/11/12
 * Time: 18:20
 */

namespace Catering\Controller;
use Think\Controller;
//餐饮娱乐商家后台登录
class LoginController extends Controller
{
    public function Login(){
        $this->display();
    }

    /**
     * 生成验证码
     */
    public function verify(){
        $config =    array(
            'fontSize'    =>    30,    // 验证码字体大小
            'length'      =>    4,     // 验证码位数
            'useNoise'    =>    false, // 关闭验证码杂点
        );
        $Verify =     new \Think\Verify($config);
        $Verify->entry();
    }

    //商家登录
    public function save(){
        $username = isset($_POST['username']) ? trim($_POST['username']) : '';
        $password = isset($_POST['password']) ? trim($_POST['password']) : '';
        $code = isset($_POST['code']) ? trim($_POST['code']) : '';
        if ($username && $password == '') {
            $arr = array(
                "code"=>4,
                "msg"=>"用户名密码必填",
            );
            $this->ajaxReturn($arr);
        }
        if (!$this->check_verify($code)) {
            $arr = array(
                "code"=>3,
                "msg"=>"验证码错误",
            );
            $this->ajaxReturn($arr);
        }
        $map['uid'] =$username;
        $map['pwd'] = md5($password);
        $user =  M("catering")->where($map)->find();
        if($user){
            session('merchant',$user);
            cookie('merchant',$user);
            $arr = array(
                "code"=>0,
                "msg"=>"登录成功",
            );
        }else{
            $arr = array(
                "code"=>"1",
                "msg"=>"您输入的账号或密码不对",
            );
        }
        $this->ajaxReturn($arr);
    }

    /**
     * 修改密码页面
     */
    public function psw(){
        $this->display('Login/Password');//渲染视图
    }


    /**
     * 修改密码
     */
    public function pswsave()
    {
        $id = session('merchant.id');       /* 登录ID */
        $pwd = session('merchant.pwd');     /* 登录密码 */
        $get = $_GET;
        $newPwd = md5($get['password']);
        $user = M('catering');
        if($pwd != $newPwd){
            $arr = array(
                "code"=>1,
                "msg"=>"您输入的当前密码有误",
            );
            $this->ajaxReturn($arr);
        }elseif(md5($get['password']) == md5($get['pwd'])){
            $arr = array(
                "code"=>0,
                "msg"=>"新密码和旧密码一致",
            );
            $this->ajaxReturn($arr);
        }
        $arr['pwd'] = md5($get['pwd']);
        $data = $user->where("id = $id")->save($arr);
        if($data){
            $arr = array(
                "code"=>200,
                "msg"=>"修改密码成功",
            );
            session(null);
        }else{
            $arr = array(
                "code"=>0,
                "msg"=>"修改密码失败",
            );
        }
        $this->ajaxReturn($arr);


    }


    /**
     * 退出登录
    */
    public function loginOut()
    {
        $session = isset($_SESSION['merchant']);
        session(null);
        $this->redirect('Login/Login');
    }


    /**
     * @param $code
     * @param string $id
     * @return bool
     * 验证码检测
     */
    function check_verify($code, $id = '')
    {
        $verify = new \Think\Verify();
        return $verify->check($code, $id);
    }
}